<?php

namespace App\HttpController\Api\Admin\Security;

use App\HttpController\Api\Admin\AdminBase;
use App\Lib\Utils;
use App\Model\Face\CompareModel;
use EasySwoole\Http\Message\Status;

class Comparison extends AdminBase
{

    public function index()
    {
        $model = new CompareModel();
        $str = $this->raw;
        $model->params = json_decode(Utils::characet($str),true);
        $data = $model->getList();
        if (!$data) {
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error".$model->message);
        }

        $this->response()->withHeader('Content-Type', 'application/json;charset=utf-8');
        $this->response()->write(json_encode([
            'code' => 0,
            'data' => $data['lists'],
            'count' => $data['count'],
        ]));
    }

    public function export()
    {
        global $_W;

        $model = new CompareModel();
        $str = $this->raw;
        $model->params = json_decode(Utils::characet($str),true);
        $condition = $model->setCondition();

        if (isset($condition['A.capture_time']) && !empty($condition['A.capture_time'])) {
            $year = date('Y', $condition['A.capture_time'][0][0]);
        } else {
            $year = date('Y', time());
        }

        try {
            $columns = 'A.imgpath, A.mode, A.compare_result, 
                    A.name, E.person_code, D.name as group_name,
                    A.device_uuid,
                    C.name as device_name,  A.temper, A.channel, 
                    A.capture_time, A.create_time';

            $tableName = "sc_compare_{$_W['companyid']}_{$year}";
            $tableName2 = "sc_compare_info_{$_W['companyid']}_{$year}";

            $model->tableName($tableName);
            $data = $model->getAllCompare($tableName2, $columns, $condition);

            if (count($data) > 3000) {
                return $this->writeJson(Status::CODE_BAD_REQUEST, '每次导出记录应小于3000条', []);
            }

            $sex = ['1' => '男', '2' => '女'];
            $modeList = ['0' => '陌生人', '1' => '黑名单', '2' => '员工', '3' => 'VIP'];

            foreach ($data as &$list) {
                $list['create_time'] = date("Y-m-d H:i:s", $list['create_time']);
                $list['capture_time'] = date("Y-m-d H:i:s", $list['capture_time']);
                $list['compare_result'] = ($list['compare_result']*100).'%';
                $list['mode'] = $modeList[$list['mode']] ?? '';
                $list['channel'] += 1;
            }

            $filename = '对比记录列表-'.$_W['companyid'].'-'.date('ymdhis').'.xls';
            $filepath = 'excel/'.$filename;
            $titles = ['人脸底图', '名单类型', '相似度', '姓名', '编号', '设备分组', '设备序列号', '设备名称', '体温', '通道号', '抓拍时间', '记录时间'];
            $model->exportExcel($data, $filename, $titles);

            $this->response()->write(file_get_contents(EASYSWOOLE_ROOT.'/webroot/excel/'.$filename));
            $this->response()->withHeader('Content-type', 'application/vnd.ms-excel;charset=UTF-8');
            $this->response()->withHeader('Content-Disposition', 'attachment;filename="test.xls"');
            $this->response()->withHeader('Cache-Control','max-age=0');
            $this->response()->end();

        } catch(\Exception $e) {
            echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, 'Server Error '.$e->getMessage(), []);
        }

        return $this->writeJson(Status::CODE_LIST_OK, 'ok', $data);
    }

}